﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuanLyCayXanh.Models;

namespace QuanLyCayXanh.Controllers
{
    public class WardController : Controller
    {
        private QuanLyCayXanhContext db = new QuanLyCayXanhContext();
        //
        // GET: /Ward/

        public ActionResult Index()
        {
            var wards = db.Wards.Include(w => w.District);
            return View(wards.ToList());
        }

        public ActionResult Create()
        {
            ViewBag.District_Id = new SelectList(db.Districts, "Id", "Name");
            return View();
        }
        //
        // POST: /Ward/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Ward ward)
        {
            if (ModelState.IsValid)
            {
                db.Wards.Add(ward);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.District_Id = new SelectList(db.Districts, "Id", "Name", ward.District_Id);
            return View(ward);
        }

        public ActionResult Details(int id)
        {
            Ward ward = db.Wards.Find(id);
            if (ward == null)
                return HttpNotFound();
            return PartialView(ward);
        }

        public ActionResult Edit(int id)
        {
            Ward ward = db.Wards.Find(id);
            ViewBag.District_Id = new SelectList(db.Districts, "Id", "Name", ward.District_Id);
            
            if (ward == null)
                return HttpNotFound();
            return View(ward);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Ward ward)
        {
            if (ModelState.IsValid)
            {
                db.Entry(ward).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(ward);
        }
                
        [HttpPost, ActionName("Delete")]           
        public ActionResult DeleteConfirmed(int id)
        {
            Ward ward = db.Wards.Find(id);
            db.Wards.Remove(ward);
            db.SaveChanges();
            return Content(Boolean.TrueString);
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
